# BeatStatistics/CMakeLists.txt - 主管理源代码文件
cmake_minimum_required(VERSION 3.10)
project(BeatStatistics VERSION 1.0 LANGUAGES CXX)

# 设置默认值 192.168.31.132
set(DEFAULT_IP "127.0.0.1")
set(DEFAULT_PORT "22018")
set(DEFAULT_MAX_TOPICS 999999)  # 注意这里是整数，不是字符串

# 使用 C++11 标准
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 自动处理 Qt 相关的文件
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_BUILD_TYPE Debug)

# 包含 src 目录
add_subdirectory(src)

# 设置是否运行格式化的选项
option(RUN_FORMAT "Run astyle formatting before building" ON)

if(RUN_FORMAT)
    # 查找所有 .hpp 文件（递归）
    file(GLOB_RECURSE HPP_FILES "${CMAKE_SOURCE_DIR}/src/*.hpp")

    # 添加 astyle 格式化命令
    add_custom_target(format
        COMMAND astyle --style=java --indent=spaces=4 --suffix=none ${HPP_FILES}
        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
        COMMENT "Formatting header files with astyle"
    )

    # 使 format 目标在构建主程序之前运行
    add_dependencies(${PROJECT_NAME} format)
endif()

# 安装步骤
include(CPack)
set(CPACK_PACKAGE_NAME BeatStatistics)
set(CPACK_PACKAGE_VERSION )
set(CPACK_GENERATOR "ZIP")

# 定义安装目录和文件
install(TARGETS BeatStatistics DESTINATION bin)

if(WIN32)
    if(MSVC)
        target_compile_options(${PROJECT_NAME} PRIVATE /W0)
    else()
        target_compile_options(${PROJECT_NAME} PRIVATE -w)
    endif()
    file(GLOB DLL_FILES "${CMAKE_SOURCE_DIR}/resource/package/*.dll")
    install(FILES ${DLL_FILES} DESTINATION bin)
    install(DIRECTORY "${CMAKE_SOURCE_DIR}/resource/package/" DESTINATION bin)
endif()

install(DIRECTORY "${CMAKE_SOURCE_DIR}/resource/icons/" DESTINATION share/icons FILES_MATCHING PATTERN "*.svg")
install(DIRECTORY "${CMAKE_SOURCE_DIR}/resource/portfolio/" DESTINATION share/portfolio FILES_MATCHING PATTERN "*.xml")

set(CPACK_PACKAGE_EXECUTABLES ${PROJECT_NAME} ${PROJECT_NAME})

